﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using SampleApp.Infrastructure;

namespace SampleAppAPI
{
    public class Order : IOrder, INotifyPropertyChanged, IRaisePropertyChanged, IDirtyValueTracker<string>
    {
        #region Private Data Members
        private IEnumerable<IDataRow> _dataRows;
        private OMSOrderData _omsOrderData;
        #endregion

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region IDirtyValueTracker Implementation
        public Dictionary<string, string> UpdatedValues { get; protected set; }
        #endregion

        #region Properties
        private OMSOrderData OMSOrderData
        {
            get
            {
                return _omsOrderData ?? _dataRows.OfType<OMSOrderData>().First();
            }
        }

        public Guid OrderID
        {
            get { return OMSOrderData.OrderID; }
        }

        public string Symbol
        {
            get { return OMSOrderData.Symbol; }
        }

        public string CompanyName
        {
            get { return OMSOrderData.CompanyName; }
        }

        public double? LimitPrice
        {
            get
            {
                return OMSOrderData.LimitPrice;
            }
            set
            {
                OMSOrderData.LimitPrice = value;
                UpdatedValues[this.GetPropertyName(o => o.LimitPrice)] = value != null ? value.ToString() : string.Empty;
            }
        }
        #endregion

        public Order(IEnumerable<IDataRow> dataRows_)
        {
            _dataRows = dataRows_;
            UpdatedValues = new Dictionary<string, string>();
        }

        #region INotifyPropertyChanged implementation
        public void RaisePropertyChanged(string propertyName_)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName_));
            }
        }
        #endregion
    }
}
